package com.study.zouchao.superproject.zouchao;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.gson.Gson;
import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.login.model.entity.User;
import com.study.zouchao.superproject.login.model.entity.result.LoginResult;
import com.study.zouchao.superproject.util.App;
import com.study.zouchao.superproject.util.LogLongUtil;
import com.study.zouchao.superproject.util.StringUtils;
import com.study.zouchao.superproject.util.ToastUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import rx.internal.schedulers.ScheduledAction;

public class LoginTestActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_test);
        start();
    }

    private String mTargetUrl = "http://106.15.90.202:8080/super/rest/user/login";
    private void start() {
        /**
         * OKHttp框架: 用于网络请求 照着写就好
         */
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(mTargetUrl)
                .post(getRequestParams())           //获取请求参数
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                toastOnUiThread("登陆失败：网络连接出错");
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                onGetResult(response);
            }
        });
    }

    private void onGetResult(Response response) {
        String strResult = null;
        try {
            strResult = response.body().string();           //得到后端返回的数据
        } catch (IOException e) {
            e.printStackTrace();
            toastOnUiThread("登陆失败：网络连接出错");
        }
        if (StringUtils.isEmpty(strResult)) {
            toastOnUiThread("登陆失败：后端数据未返回");
        }
        LoginResult loginResult = new Gson().fromJson(strResult, LoginResult.class);
        judgeLoginResult(loginResult);
    }

    private void judgeLoginResult(LoginResult loginResult) {
        if (loginResult == null)    return;
        switch (loginResult.getCode()) {
            case 21001 :
                toastOnUiThread("登陆成功:"+loginResult.toString());
                break;
            default :           //根据文档除了21001以外其他都是登陆失败
                toastOnUiThread("登陆失败~~"+loginResult.getMessage());
                break;
        }
    }

    /**
     * 获取向后端请求的请求参数
     * @return
     */
    private RequestBody getRequestParams() {
        RequestBody formBody = new FormBody.Builder()
                .add("userName", "zou1234")
                .add("password", "1234")
                .build();
        return formBody;
    }

    /**
     * 弹出条Toast
     * @param title
     */
    private void toastOnUiThread(final String title) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtils.showShort(App.getAppContext(), title);
            }
        });
    }
}
